//@ sourceURL=/static/modules/employee_manage/employee_manage.js

var employeeManageView = new View({
	name: "employee_manage",
	init_page: function () {
		$('#add_btn').click(function () {
			path.employee_add = "/employee_add";
			pjaxInit('/workshop?key=employee_add');
			return false;
		});
	}
})


employeeManageView.render = {
	operation: function (data, type) {
		var html = '<button class="btn btn-primary margin-right-10 grid-edit-btn" data-name="' + data.name + '" data-id="' + data.userId + '" type="button" onClick="employeeManageView.fn.edit(this)" >编辑</button>' +
			'<button class="btn btn-primary margin-right-10 grid-delete-btn" data-name="' + data.name + '" data-id="' + data.userId + '" type="button" onClick="employeeManageView.fn.delete(this)" >删除</button>' +
			'<button class="btn btn-primary margin-right-10" data-name="' + data.name + '" data-id="' + data.userId + '" type="button" onClick="employeeManageView.fn.resetPassword(this)" >重置密码</button>';
		return html;
	}
};
//function
employeeManageView.fn = {
	edit: function (e) {
		var id = $(e).data('id');
		path.employee_add = '/user/edit/' + id;
		pjaxInit('/workshop?key=employee_add');
	},
	delete: function (e) {
		var ids = [];
		if ($(e).data('type') == 'selected-delete') {
			employeeManageView.container.find('td input[type="checkbox"]').each(function () {
				if ($(this).prop('checked')) {
					ids.push($(this).parents('tr').find('[data-id]:eq(0)').data('id'));
				}

			})
		} else {
			ids.push($(e).data('id'));
		}
		if (ids.length == 0) {
			alertify.error('请选择员工');
			return false;
		}
		var nameArr = [];
		ids.forEach(function (id) {
			nameArr.push($('[data-id=' + id + ']').data('name'));
		})
		swal($.po('sweetalert', {
			title: "确定要删除该员工吗？",
			type: "warning",
			showCancelButton: true,
			confirmButtonColor: '#DD6B55',
			confirmButtonText: '删除',
			cancelButtonText: "取消",
			closeOnConfirm: false,
		}),
			function (isConfirm) {
				if (isConfirm) {
					wdAjax({
						url: "/user/delete",
						data: ids,
						success: function (data) {
							// swal("", "员工 " + nameArr.toString() + " 已删除成功", "success");
							swal.close();
							toastr.success("员工 " + nameArr.toString() + " 已删除成功", null, { "positionClass": "toast-top-center" });
							$(".dataTables_wrapper").find('th[data-type="checkbox"]').iCheck('uncheck');
							pjaxInit('/workshop?key=employee_manage');
						}
					})
				}
			});
	},
	resetPassword: function (e) {

	}
}

//员工table 
employeeManageView.use(new Table({
	url: '/user/users',
	element: '#employee_manage_list',
	search_btn: "#search_btn",
	callback: function () {

	}
}));

//选择员工tree
employeeManageView.use(new Tree({
	container: "#wd-tree-container",
	data: departmentTree,
	select_callback: function (data) {
		//点击节点
		if (data.node.parent && data.node.parent != '#') {
			$('[name=departmentId]').val(data.node.id);
			$('#search_btn').trigger('click');
		}

	}
}));
